package cn.imvc.app.controllers.goods;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;

import cn.imvc.app.commons.SystemConfig;
import cn.imvc.app.entities.Goods;
import cn.imvc.app.entities.GoodsSpec;
import cn.imvc.app.models.GoodsModel;
import cn.imvc.app.models.GoodsSkuModel;
import cn.imvc.app.models.GoodsSpecModel;
import cn.imvc.app.models.LoggingModel;
import cn.imvc.app.models.UserModel;
import cn.imvc.app.models.UserSellerModel;
import cn.imvc.core.DialogBean;
import cn.imvc.core.Render;
import cn.imvc.data.domain.ViewRequest;

/**
 * @date 2016-7-18 15:18:00
 * @author chuangye01
 * 商品管理控制器
 */
public class GoodsControllerImpl implements GoodsController {
	static Logger logger = Logger.getLogger(GoodsControllerImpl.class);
	public Page<Goods> page;
	public Goods goods;

	public ViewRequest viewRequest;
	public int id;
	public int classId;
	public int[] ids;
	public String goodsClassName;
	public String brandName;
	public String sellerName;
	public List<GoodsSpec> goodsSpecs;
	public String isSku;
	
	
	@Autowired
	GoodsModel goodsModel;
	@Autowired
	GoodsSpecModel goodsSpecModel;
	@Autowired
	UserSellerModel userSellerModel;
	@Autowired
	UserModel userModel;
	@Autowired
	GoodsSkuModel goodsSkuModel;
	@Autowired
	SystemConfig systemConfig;
	@Autowired
	LoggingModel loggingModel;
	
	
	public void setIsSku(String isSku) {
		this.isSku = isSku;
	}

	public void setViewRequest(ViewRequest viewRequest) {
		this.viewRequest = viewRequest;
	}
	
	public void setId(int id) {
		this.id = id;
	}

	public void setIds(int[] ids) {
		this.ids = ids;
	}
	
	public void setGoodsSpecs(List<GoodsSpec> goodsSpecs) {
		this.goodsSpecs = goodsSpecs;
	}
	
	public void setGoods(Goods goods) {
		this.goods = goods;
	}

	public void setGoodsClassName(String goodsClassName) {
		this.goodsClassName = goodsClassName;
	}
	
	public void setBrandName(String brandName) {
		this.brandName = brandName;
	}
	
	public void setSellerName(String sellerName) {
		this.sellerName = sellerName;
	}

	public String search() {
		// TODO Auto-generated method stub
		return null;
	}

	public String index() {
		page=goodsModel.find(viewRequest);
		return "goods/index";
	}

	public String show() {
		// TODO Auto-generated method stub
		return null;
	}

	public String editNew() {
		goods = new Goods();
		goods.setAddTime(new Date());
		return "goods/edit";
	}

	public String create() {
		goods = goodsModel.save(goods);
		loggingModel.info(logger,"添加商品："+goods.getName());
		return Render.json("{'statusCode':'200','message':'创建成功，等待审核','version':'"+goods.getVersion()+"','goodsId':'"+goods.getId()+"'}");
	}

	public String edit() {
		goods=goodsModel.get(id);
//		List<GoodsSku> goodsSkus = goodsSkuModel.findByGoods(goods);
//		Iterator<GoodsSku> iterator = goodsSkus.iterator();
//		int sumInventory = 0;
//		int sumCollect = 0;
//		int sumSale = 0;
//		int sumClick = 0;
//		while (iterator.hasNext()) {
//			GoodsSku goodsSku = (GoodsSku) iterator.next();
//			sumInventory += goodsSku.getInventory() == null ? 0 : goodsSku.getInventory();
//			sumCollect += goodsSku.getCollect() == null ? 0 : goodsSku.getCollect();
//			sumSale += goodsSku.getSalenum() == null ? 0 : goodsSku.getSalenum();
//			sumClick += goodsSku.getClick() == null ? 0 : goodsSku.getClick();
//		}
//		goods.setClick(sumClick);
//		goods.setSalenum(sumSale);
//		goods.setCollect(sumCollect);
//		goods.setInventory(sumInventory);
//		goodsSpecs = goodsSpecModel.findByGoods(goods);
//		goodsSpecModel.find(viewRequest);
		return "goods/edit";
	}

	public String update() {
		goods = goodsModel.updateProperties(goods,true);
		loggingModel.info(logger,"更新商品："+goods.getName());
		return Render.json("{'statusCode':'200','message':'更新成功，等待审核','version':'"+goods.getVersion()+"','goodsId':'"+goods.getId()+"'}");
	}

	public String destroy() {
		String result = goodsModel.delete(id);
		if(result.equals("")){
			loggingModel.info(logger,"删除商品："+id);
			return Render.json(new DialogBean("200","删除成功",""));
		}else{
			return Render.json(new DialogBean("300", result,"")); 
		}
	}

	public String destroyBatch() {
		String result = goodsModel.deleteBatch(ids);
		if(result.equals("")){
			return Render.json(new DialogBean("200","删除成功",""));
		}else{
			return Render.json(new DialogBean("300", result,"")); 
		}
	}

	public String dialog() {
		if(viewRequest == null){
			viewRequest = new ViewRequest();
		}
		if(viewRequest.getParams() == null){
			viewRequest.setParams(new HashMap<>());
		}
		viewRequest.getParams().put("_and_g_dot_verify_eq_string", "1");
		viewRequest.getParams().put("_and_g_dot_state_eq_string", "1");
		page=goodsModel.find(viewRequest);
		return "goods/dialog";
	}

	public String dialog_sku(){
		isSku = "1";
		dialog();
		return "goods-sku/dialog_goods";
	}
}
